package cn.wj.school.cloud.security.context;

import org.springframework.util.Assert;


/**
 * @author lidapeng
 */
public final class ThreadLocalSecurityContextHolderStrategy implements SecurityContextHolderStrategy {

//~ Static fields/initializers =====================================================================================

  private static final ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<SecurityContext>();

  //~ Methods ========================================================================================================

  @Override
  public void clearContext() {
      contextHolder.remove();
  }

  @Override
  public SecurityContext getContext() {
      SecurityContext ctx = contextHolder.get();

      if (ctx == null) {
          ctx = createEmptyContext();
          contextHolder.set(ctx);
      }

      return ctx;
  }

  @Override
  public void setContext(SecurityContext context) {
      Assert.notNull(context, "Only non-null SecurityContext instances are permitted");
      contextHolder.set(context);
  }

  @Override
  public SecurityContext createEmptyContext() {
    return new SecurityContextImpl();
  }

}
